// 文件路径: src/main/java/com/example/demo22/mapper/UserMapper.java

package com.example.demo22.mapper;

import com.example.demo22.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM user WHERE username = #{username} AND password = #{password}")
    User findUserByUsernameAndPassword(@Param("username") String username, @Param("password") String password);

    @Insert("INSERT INTO user (username, password, email, sex, role, last_login_time, login_count) " +
            "VALUES (#{user.username}, #{user.password}, #{user.email}, #{user.sex}, #{user.role}, NOW(), 1)")
    void addUser(@Param("user") User user);

    @Delete("DELETE FROM user WHERE id = #{userId}")
    void deleteUserById(@Param("userId") Long userId);

    @Update("UPDATE user SET password = #{password} WHERE id = #{userId}")
    void updatePassword(@Param("userId") Long userId, @Param("password") String password);

    @Update("UPDATE user SET last_login_time = NOW(), login_count = login_count + 1 WHERE id = #{userId}")
    void updateLoginInfo(@Param("userId") Long userId);

    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByUsername(@Param("username") String username);

    @Select("SELECT * FROM user")
    List<User> findAll();

    @Update("UPDATE user SET username = #{user.username}, email = #{user.email}, sex = #{user.sex} WHERE id = #{user.id}")
    void updateUser(@Param("user") User user);
}
